﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Web.Script.Serialization;


/// <summary>
/// Summary description for ClassName
/// </summary>
public class CourierManager
{
    public CourierManager()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    
    public static int CreateCourier(Courier c){
        
        string sql = @"INSERT INTO CourierProfile(
            Code,
            Name,
            AuthContactPerson,
            AuthEmail,
            Active,
            DateCreated,
            DateUpdated)
            VALUES(@0,@1,@2,@3,@4,@5,@6)";
           List<object> param = new List<object>();
           
            param.Add(c.Code);
            param.Add(c.Name);
            param.Add(c.AuthContactPerson);
            param.Add(c.AuthEmail);
            param.Add(c.Active);
            param.Add(c.DateCreated);
            param.Add(c.DateUpdated);

          var result = DBManager.Execute(sql,param.ToArray());
          return result;
          
    }
    
     public static dynamic CheckExistCP(string code, string name){

         string sql = "SELECT * FROM CourierProfile WHERE Code=@0 AND Name=@1";
        object[] param = new object[2];
            param[0]= code;
            param[1]= name;
            
        var query = DBManager.Query(sql,param);
            if(query.Count>0){
                return query[0];
            }else{
                return null;    
            }
         

    }
    
    
    
    public static IList<dynamic> GetCourierLkUpValues(){
        string sql = "SELECT * FROM CourierProfile";
        var query=DBManager.Query(sql,null);
        return query;
    }
    
    
     public static IList<dynamic> SearchCourier(Courier c){
            try{
                var sql = string.Format("SELECT * FROM CourierProfile WHERE Active={0}",c.Active);
                Dictionary<string,string> param = new Dictionary<string,string>();
                StringBuilder sb = new StringBuilder();
                sb.Append(sql);
                
                string cond=" AND ";
                
                if(c.Code!=""){
                    sb.Append(cond).Append(string.Format("Code LIKE '{0}%'",c.Code));     
                }
                
                if(c.Name!=""){
                    sb.Append(cond).Append(string.Format("Name LIKE '{0}%'",c.Name));     
            
                }
                
                var query = DBManager.Query(sb.ToString(),null);
                return query;
               
                
            }catch(Exception e){
                throw e;
            }
        }
    
     public static dynamic GetCourier(int id){
            try{
                var sql = string.Format("SELECT * FROM CourierProfile WHERE ID={0}",id);
                var courier = DBManager.Query(sql,null);
                
                return courier[0];
                
            }catch(Exception e){
                throw e;
            }
        }
    
     public static int UpdateCP(Courier c){
        
        string sql = @"UPDATE  CourierProfile
            SET Code=@0,
            Name=@1,
            AuthContactPerson=@2,
            AuthEmail=@3,
            Active=@4,
            DateUpdated=@5
            WHERE ID=@6";
           List<object> param = new List<object>();
           
            param.Add(c.Code);
            param.Add(c.Name);
            param.Add(c.AuthContactPerson);
            param.Add(c.AuthEmail);
            param.Add(c.Active);
            param.Add(c.DateUpdated);
            param.Add(c.ID);

          var result = DBManager.Execute(sql,param.ToArray());
          return result;
          
    }
    

}
